package com.njkwt.business.system.controller;

import com.njkwt.business.system.dto.UploadFileDto;
import com.njkwt.business.system.dto.PageDto;
import com.njkwt.business.system.dto.ResponseDto;
import com.njkwt.business.system.service.UploadFileService;
import com.njkwt.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* @ClassName UploadFileController
* @Description
* @Author ydj (171799383@qq.com)
* @Date  2020-8-18 14:45:23
* @Version V1.0
**/

@RestController
@RequestMapping("/api/business/system/uploadFile")
public class UploadFileController {

    private static final Logger LOG = LoggerFactory.getLogger(UploadFileController.class);
    public static final String BUSINESS_NAME = "文件";

    @Resource
    private UploadFileService uploadFileService;

    /**
     * 列表查询
     */
    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();
        uploadFileService.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody UploadFileDto uploadFileDto) {
        // 保存校验
        ValidatorUtil.require(uploadFileDto.getId(), "Id");
        ValidatorUtil.length(uploadFileDto.getId(), "Id", 1, 50);
        ValidatorUtil.require(uploadFileDto.getFilename(), "文件名称");
        ValidatorUtil.length(uploadFileDto.getFilename(), "文件名称", 1, 200);
        ValidatorUtil.require(uploadFileDto.getFilepath(), "文件路径");
        ValidatorUtil.length(uploadFileDto.getDescription(), "描述", 1, 200);
        ValidatorUtil.length(uploadFileDto.getFiletype(), "文件类型", 1, 20);
        ValidatorUtil.length(uploadFileDto.getExtension(), "扩展名称", 1, 20);
        ValidatorUtil.require(uploadFileDto.getEnable(), "是否可用");
        ValidatorUtil.require(uploadFileDto.getSortcode(), "排序");
        ValidatorUtil.require(uploadFileDto.getDeletemark(), "删除标识");
        ValidatorUtil.length(uploadFileDto.getCreateusername(), "上传人姓名", 1, 50);
        ValidatorUtil.require(uploadFileDto.getCreatetime(), "上传时间");
        ValidatorUtil.length(uploadFileDto.getBelongapp(), "所属应用", 1, 200);
        ValidatorUtil.length(uploadFileDto.getBelongappid(), "所属应用ID", 1, 50);

        ResponseDto responseDto = new ResponseDto();
        uploadFileService.save(uploadFileDto);
        responseDto.setContent(uploadFileDto);
        return responseDto;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        uploadFileService.delete(id);
        return responseDto;
    }
}
